/* Copyright 2021 Kerim Yalcin
**
**   Licensed under the Apache License, Version 2.0 (the "License");
**   you may not use this file except in compliance with the License.
**   You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
**   Unless required by applicable law or agreed to in writing, software
**   distributed under the License is distributed on an "AS IS" BASIS,
**   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
**   See the License for the specific language governing permissions and
**   limitations under the License.
*/

#include "File.hpp"

Agx::File::File() {
    mDesiredAccess = GENERIC_READ | GENERIC_WRITE;
    mShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
    mCreationDisposition = CREATE_NEW;
}


void Agx::File::enable_DesiredAccess_Read() {
    mDesiredAccess |= GENERIC_READ;
}

void Agx::File::disable_DesiredAccess_Read() {
    mDesiredAccess ^= GENERIC_READ;
}

void Agx::File::enable_DesiredAccess_Write() {
    mDesiredAccess |= GENERIC_WRITE;
}

void Agx::File::disable_DesiredAccess_Write() {
    mDesiredAccess ^= GENERIC_WRITE;
}


const DWORD& Agx::File::get_DesiredAccess_coded() {
    return mDesiredAccess;
}

const std::string Agx::File::get_DesiredAccess_stringed() {
    std::string out;

    if((mDesiredAccess & GENERIC_WRITE) == GENERIC_WRITE) {
        out.append("Generic Write");
    }
    if((mDesiredAccess & GENERIC_READ) == GENERIC_READ) {
        if(!out.empty())
            out.append(" | ");
        out.append("Generic Read");
    }

    return out;
}


void Agx::File::enable_FileShare_Delete() {
    mShareMode |= FILE_SHARE_DELETE;
}

void Agx::File::disable_FileShare_Delete() {
    mShareMode ^= FILE_SHARE_DELETE;
}

void Agx::File::enable_FileShare_Read() {
    mShareMode |= FILE_SHARE_READ;
}

void Agx::File::disable_FileShare_Read() {
    mShareMode ^= FILE_SHARE_READ;
}

void Agx::File::enable_FileShare_Write() {
    mShareMode |= FILE_SHARE_WRITE;
}

void Agx::File::disable_FileShare_Write() {
    mShareMode ^= FILE_SHARE_WRITE;
}


void Agx::File::set_CreationDisposition_CreateAlways() {
    mCreationDisposition = CREATE_ALWAYS;
}

void Agx::File::set_CreationDisposition_CreateNew() {
    mCreationDisposition = CREATE_NEW;
}

void Agx::File::set_CreationDisposition_OpenAlways() {
    mCreationDisposition = OPEN_ALWAYS;
}

void Agx::File::set_CreationDisposition_OpenExisting() {
    mCreationDisposition = OPEN_EXISTING;
}

void Agx::File::set_CreationDisposition_TruncateExisting() {
    mCreationDisposition = TRUNCATE_EXISTING;
}


const DWORD& Agx::File::get_CreationDisposition_coded() { return mCreationDisposition; }
const std::string Agx::File::get_CreationDisposition_stringed() { return std::string(); }



void Agx::File::set_Directory(const std::string& pDir) {

}

void Agx::File::set_Extension(const std::string& pExt) {

}

void Agx::File::set_FileName(const std::string& pName) {

}

void Agx::File::create_File() {
    CreateDirectoryA(
        "E:",
        nullptr
    );

    std::cout << "CreateDirectoryA: " << (unsigned int)GetLastError() << "\n";

    mFileHandle =
        CreateFileA(
            "C:/Users/agaxl/Desktop/Test/Test2.txt",
            mDesiredAccess,
            mShareMode,
            nullptr,
            mCreationDisposition,
            FILE_ATTRIBUTE_NORMAL,
            nullptr
        );

    std::cout << "CreateFileA: " << (unsigned int)GetLastError() << "\n";

}